package com.example.beikeyigou;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.beikeyigou.custom.HorizontalListView;
import com.example.beikeyigou.custom.NavitationFollowScrollLayout;
import com.example.beikeyigou.custom.ViewPagerAdapter;
import com.example.beikeyigou.goods.GoodsTypeFragment;
import com.example.beikeyigou.goods.TypeAdapter;
import com.example.beikeyigou.goods.TypeBean;
import com.example.beikeyigou.search.SearchActivity;
import com.example.beikeyigou.utils.BaseFragment;
import com.example.beikeyigou.utils.URLUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * 分类
 */
public class TypeFragment extends BaseFragment {
//    HorizontalListView typeLv;
    NavitationFollowScrollLayout typeBar;
    ViewPager viewPager;
    private LinearLayout searchBar;
    private ViewPagerAdapter viewPagerAdapter;
    private List<Fragment> fragments;

    EditText searchEt;                  // 搜索框
    List<TypeBean.DataBean> data;       // 数据源
    private TypeAdapter adapter;
    private FragmentManager fm;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_type, container, false);
        initView(view);
        // 初始化数据
        initData();
        // 刷新数据
        refreshData();
        setListener();
        return view;
    }

    private void initData() {
        data = new ArrayList<>();
        adapter = new TypeAdapter(getContext(), data);
        fm = getChildFragmentManager();

//        fragments =  new ArrayList<>();
//        fm = getChildFragmentManager();
//        viewPagerAdapter = new ViewPagerAdapter(fm, fragments);
//        viewPager.setAdapter(viewPagerAdapter);

//        typeLv.setAdapter(adapter);
//        fm = getChildFragmentManager();
        // 设置监听事件
        setListener();
    }

    //点击上面的item切换下面的页面
//    private void changeBelowPage (int position) {
//        TypeBean.DataBean dataBean = data.get(position);
//        int id = dataBean.getId();
//        FragmentTransaction transaction = fm.beginTransaction();
//        GoodsTypeFragment gtFrag = GoodsTypeFragment.newInstance(id + "", "");
//        transaction.replace(R.id.frag_type_layout, gtFrag);
//        transaction.commit();
//    }

    private void refreshData() {
        // 连接网络
        getNetwork(URLUtils.queryAllCategory_url);
    }

    private void initView(View view) {
//        typeLv = view.findViewById(R.id.type_listView);
        typeBar = view.findViewById(R.id.frag_type_bar);
        viewPager = view.findViewById(R.id.frag_type_viewPaper);
        searchBar = view.findViewById(R.id.frag_type_search);
    }

    // 给listView的每一项设置点击事件
    private void setListener() {
//        typeLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                adapter.setClickPos(position);
//                changeBelowPage(position);
//            }
//        });
        searchBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
            }
        });

        typeBar.setOnTitleClickListener(new NavitationFollowScrollLayout.OnTitleClickListener() {
            @Override
            public void onTitleClick(View v) {

            }
        });

        typeBar.setOnNaPageChangeListener(new NavitationFollowScrollLayout.OnNaPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        TypeBean typeBean = new Gson().fromJson(result, TypeBean.class);    // 解析数据
        List<TypeBean.DataBean> list = typeBean.getData();                  // 获取网络数据集合
        data.addAll(list);                  // 将网络数据添加到list view的数据源中

        fragments =  new ArrayList<>();

        int titleNum = data.size();
        String[] title = new String[titleNum];
        int id;
        for (int i = 0; i < titleNum; ++i) {
            title[i] = data.get(i).getName();
            id = data.get(i).getId();
            fragments.add(GoodsTypeFragment.newInstance(id + "", ""));
        }
        viewPagerAdapter = new ViewPagerAdapter(fm, fragments);
        viewPager.setAdapter(viewPagerAdapter);
        typeBar.setViewPager(getContext(), title, viewPager, R.color.black, R.color.pink, 15, 18, 12, true, R.color.color_333333, 0f, 15f, 15f, 80);
        typeBar.setBgLine(getContext(), 1, R.color.colorAccent);
        typeBar.setNavLine(getActivity(), 3, R.color.pink);
        viewPagerAdapter.notifyDataSetChanged();
    }
}